/*
 * UserBean.java
 */
package ITU_ShuttleTracker.User;

import ITU_ShuttleTracker.Database;
import ITU_ShuttleTracker.User.Location.Location;
import ITU_ShuttleTracker.User.ShuttleBus.ShuttleBus;
import java.io.IOException;
import java.io.Serializable;  
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;
import org.primefaces.model.map.Polyline;
  
@ManagedBean(name="UserBean")
@SessionScoped  

/**
 *
 * @author Onuralp
 */
public class UserBean implements Serializable {  
    private MapModel polylineModel;
    private Location userLocation;
    private List<ShuttleBus> shuttleBusList;
    private User user;
    private Marker marker;
    private String information;

    /**
     * Constructor of UserBean class
     */
    public UserBean() {
        this.userLocation = new Location("Konumunuz", 41.10377863119175, 29.024966041610696);
        this.user = new User(this.userLocation);
        this.polylineModel = new DefaultMapModel(); 
        this.shuttleBusList = new ArrayList<ShuttleBus>();
        this.information = "";
        Database database = new Database();
        try {
            database.connect();
            this.user.setRoute(database.selectRoute());
            this.user.setScheduleList(database.selectSchedule());
            this.shuttleBusList = database.selectShuttleBus();
            database.disconnect();
        } catch (Exception exception) {
            System.out.println(exception.toString());
        }
    }
 
    /**
     * Getter method of user
     * @return User
     */
    public User getUser() {
        return user;
    }

    /**
     * Setter method of user
     * @param user User
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * Getter method of user location
     * @return User location
     */
    public Location getUserLocation() {
        return userLocation;
    }

    /**
     * Setter method of user location
     * @param userLocation User location 
     */
    public void setUserLocation(Location userLocation) {
        this.userLocation = userLocation;
    }

    /**
     * Getter polyline model method 
     * @return Polyline model
     */
    public MapModel getPolylineModel() {  
        return polylineModel;  
    }
    
    /**
     * Setter polyline model method
     * @param polylineModel Polyline model
     */
    public void setPolylineModel(MapModel polylineModel) {
        this.polylineModel = polylineModel;
    }

    /**
     * Getter shuttle bus list method 
     * @return Shuttle bus list
     */
    public List<ShuttleBus> getShuttleBusList() {
        return shuttleBusList;
    }

    /**
     * Setter shuttle bus list method 
     * @param shuttleBusList Shuttle bus list
     */
    public void setShuttleBusList(List<ShuttleBus> shuttleBusList) {
        this.shuttleBusList = shuttleBusList;
    }
    
    /**
     * Getter selected marker method
     * @return Selected marker
     */
    public Marker getMarker() {  
        return marker;  
    }

    /**
     * Setter information method
     * @return Information
     */
    public String getInformation() {
        //showShuttleTrackerInformation();
        return information;
    }

    /**
     * Getter information method
     * @param information Information 
     */
    public void setInformation(String information) {
        this.information = information;
    }
    
    /**
     * Show route method
     */
    public String showRoute() {
        List<LatLng> coordinateList = new ArrayList<LatLng>();
        Database database = new Database();
        try {
            database.connect();
            coordinateList = database.selectGPS();
            database.disconnect();
        } catch (Exception exception) {
            System.out.println(exception.toString());
        } 
        Polyline polyline = new Polyline();
        for (Location location : this.user.getRoute().getStationList()) {
            this.polylineModel.addOverlay(new Marker(location.getCoordinate(), location.getName(), "", "http://maps.google.com/mapfiles/ms/micons/yellow-dot.png"));
        }
        for (LatLng coordinate : coordinateList) {
            polyline.getPaths().add(coordinate);
        }
        polyline.setStrokeWeight(10);  
        polyline.setStrokeColor("#FF9900");  
        polyline.setStrokeOpacity(0.7);  
        this.polylineModel.addOverlay(polyline);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("faces/route.xhtml");
	} catch (IOException e) {
            System.out.println(e.toString());
	}
        return "";
    }
  
    /**
     * Select polyline method
     * @param event Overlay select event
     */
    public void onPolylineSelect(OverlaySelectEvent event) {  
        addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Rota seçildi.", null));  
    }  
      
    /**
     * Add message method
     * @param message Faces message
     */
    public void addMessage(FacesMessage message) {  
        FacesContext.getCurrentInstance().addMessage(null, message);  
    }  
    
    /**
     * Show schedule method
     */
    public String showSchedule() {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("faces/schedule_PG.xhtml");
	} catch (IOException e) {
            System.out.println(e.toString());
	}
        return "";
    }
    
    /**
     * Show shuttle bus list method
     */
    public void showShuttleBusList() {
        Database database = new Database();
        try {
            database.connect();
            this.shuttleBusList = database.selectShuttleBus();
            database.disconnect();
        } catch (Exception exception) {
            System.out.println(exception.toString());
        }
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	} catch (IOException e) {
            System.out.println(e.toString());
	}
    }
} 
